package com.itheima.studenthomework.controller;

import com.itheima.studenthomework.service.UserService;

import java.util.Scanner;

public class LoginController {

    public void login() {
        Scanner sc = new Scanner(System.in);

        loop:
        while (true) {
            boolean flag = false;
            System.out.println("=========欢迎来到黑马管理系统=========");
            System.out.println("1.账号登录");
            System.out.println("2.手机登录");
            System.out.println("3.返回上一级");
            String str = sc.next();

            switch (str){
                case "1":
                    flag = usernameLogin();
                    break;
                case "2":
                    flag = phoneLogin();
                    break;
                case "3":
                    break loop;
                default:
                    System.out.println("您的输入有误，请重新输入！");
                    break;
            }
            if (flag) {
                HeimaController hec = new HeimaController();
                hec.start();
            }
        }
    }

    private boolean phoneLogin() {
        Scanner sc = new Scanner(System.in);

        UserService userService = new UserService();
        System.out.println("请输入账号名：");
        String username;
        /*
        一、判断用户名是否存在：
            1.存在：放行
            2.不存在：询问是否注册
         */
        loop1:while (true) {
            username = sc.next();
            char[] chr = username.toCharArray();
            for (int i = 0; i < chr.length; i++) {
                if(chr[i] < '0' || chr[i] > '9'){
                    System.out.println("存在非法字符，请重新输入");
                    continue loop1;
                }
            }
            if(chr.length != 11){
                System.out.println("您输入的位数不正确，请重新输入");
                continue;
            }

            int index = userService.isExist(username);
            if(index != -1){
                break;
            }else {
                RegisterController.registerOrNotPhone();
                return false;
            }
        }
        /*
        二、验证码：
        1.错误：提示并给予3次机会，用完返回选择界面
        2.正确：登录
         */
        String vcd = RegisterController.verifyCode(6);
        System.out.println(vcd);
        System.out.println("请输入验证码");
        for (int i = 0; i < 3; i++) {
            String vcd2 = sc.next();
            if(vcd.equalsIgnoreCase(vcd2)){
                break;
            }else{
                if(i==2){
                    System.out.println("给你机会不中用啊，下次再来吧。");
                    return false;
                }else{
                    System.out.println("验证码输入错误，今日还剩"+(2-i)+"次机会");
                }
            }
        }
        return true;
    }

    private boolean usernameLogin() {
        Scanner sc = new Scanner(System.in);

        UserService userService = new UserService();
        System.out.println("请输入您的账号：");
        /*
        一、检查用户名：
        1.不存在：提示并返回选择界面
        2.存在：放行
         */
        int index;
        while (true){
            String username = sc.next();
            index = userService.isExist(username);
            if(index == -1){
                RegisterController.registerOrNotUser();
                return false;
            }else {
                break;
            }
        }
        /*
        二、检查密码：
        1.错误：提示并给予3次机会，用完返回选择界面
        2.正确：放行
         */
        System.out.println("请输入密码");
        for (int i = 0; i < 3; i++) {
            String password = sc.next();
            boolean flag = userService.verifyPassword(index,password);
            if(flag){
                break;
            }else{
                if(i==2){
                    System.out.println("给你机会不中用啊，下次再来吧。");
                    return false;
                }else{
                    System.out.println("密码输入错误，今日还剩"+(2-i)+"次机会");
                }
            }
        }
        /*
        三、验证码：
        1.错误：提示并给予3次机会，用完返回选择界面
        2.正确：登录
         */
        String vcd = RegisterController.verifyCode(4);
        System.out.println(vcd);
        System.out.println("请输入验证码");
        for (int i = 0; i < 3; i++) {
            String vcd2 = sc.next();
            if(vcd.equalsIgnoreCase(vcd2)){
                break;
            }else{
                if(i==2){
                    System.out.println("给你机会不中用啊，下次再来吧。");
                    return false;
                }else{
                    System.out.println("验证码输入错误，今日还剩"+(2-i)+"次机会");
                }
            }
        }
        return true;
    }

}
